<template>
  <!-- 单个商品卡片 -->
  <li class="good-card"  >
    <img :src="good.s_goods_photos[0].path" alt=""  @click="goDetail(good.id)" >
    <p>{{good.name}}</p>
    <!-- 商品描述  -->
    <p class="content-box">{{good.desc}}</p>
    <p>￥{{good.price.toFixed(2)}}</p>
    <div class="btns" >
      <!-- <button @click="goDetail(good.id)">查看详情</button> -->
      <button @click="addCart(good.id)"><i class="el-icon-shopping-cart-full"></i> 添加至购物车</button>
    </div>
    <el-dialog title="添加成功" :visible.sync="dialogVisible" width="30%">
      <span>商品已成功加入购物车！</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">继续添加商品</el-button>
        <el-button type="danger" @click="goCar">去购物车结算</el-button>
      </span>
    </el-dialog>
  </li>


</template>

<script>
import { addCar } from '@/assets/request';


export default {
  //子组件名字
  name: 'GoodCard',

  data() {
    return {

      //弹框组件可视化 初始值为false不显示
      dialogVisible: false,

    }
  },


  //接收数据---父传子，从ClassifyList.vue接收传过来的数据
  props: ["good"],

  //生命钩子
  mounted() {
    // console.log(this.$props.good);  查看层级获取所需信息在哪里
  },

  //方法
  methods: {
    //跳转到商品详情页
    goDetail(id) {
      this.$router.push({
        name: "detail",
        params: {
          id,
        },
      });
    },

    //添加购物车--把商品id传入
    async addCart(id) {
      // console.log(id);

      //调用addCar请求函数，把addCar函数需要的属性和参数安装模板填写好，把点击行为发生后需要修改的数据传给请求地址，打印变量，确认状态码为200，地址的购物车信息和数量已更新
      let add = await addCar({
        goods_id: id,
        num: 1,
        project_id: "267",
      })

      // console.log(add);

      this.dialogVisible = true

    },

    goCar() {
      this.$router.push({
        name:'cart',
      })

    }

  }
}

</script>

<style lang='scss' scoped>
li {
  list-style: none;
  width: 300px;
  // border: 1px solid #333;
  padding-bottom: 10px;
  margin-bottom: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  cursor: pointer;

  &:hover{
    box-shadow: 8.8px 5.8px 10.2px rgba(0, 0, 0, 0.5);
    background-color: white;
    transition: all 0.5s;
    .btns{
      opacity: 1;
  
    }
    
  }
  

  // box-shadow: 2.8px 2.8px 2.2px rgba(0, 0, 0, 0.02),
  //   6.7px 6.7px 5.3px rgba(0, 0, 0, 0.028),
  //   12.5px 12.5px 10px rgba(0, 0, 0, 0.035),
  //   22.3px 22.3px 17.9px rgba(0, 0, 0, 0.042),
  //   41.8px 41.8px 33.4px rgba(0, 0, 0, 0.05),
  //   100px 100px 80px rgba(0, 0, 0, 0.07);
  img {
    width: 280px;
    height: 280px;
    display: block;
  }

  p {
    width: 100%;
    font-style: 16px;
    text-align: center;
    padding-left: 10px;
    padding-right: 10px;
    margin-bottom: 10px;

    &:first-of-type {
      color: #333;
      font-weight: bold;
    }

    &:nth-of-type(2) {
      font-size: 12px;
      color: grey;
    }

    &:nth-of-type(3) {
      font-size: 16px;
      color: red;
    }
  }

  //多行文本溢出处理
  // .content-box {
  //   display: -webkit-box;
  //   display: -moz-box;
  //   overflow: hidden;
  //   // 溢出的文字显示为...
  //   text-overflow: ellipsis;
  //   //控制行数
  //   -webkit-line-clamp: 1;
  // }
  .btns {
    width: 100%;
    // display: flex;
    // justify-content: space-between;
    padding: 0px 40px;
    opacity: 0;

    button {
      width: 270px;
      height: 40px;
      background-color: transparent;
      font-size: 12px;
      // border: 1px solid #333333;
      border: none;
      cursor: pointer;
      font-size: 16px;
     
      

      i{
        font-size: 30px;
      }

      &:hover{
        background-color: #333333;
        color: white;
        transition: all 0.5s;
      }

     
    }
  }
}
</style>
